package wibrst.loading {
	import flash.display.DisplayObjectContainer;
	import flash.display.LoaderInfo;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.net.URLLoader;
	import wibrst.events.EDataTransfer;
	import wibrst.layout.GlbLayout;
	import wibrst.loading.LoadUnit;
	import wibrst.utils.EnvUtil;
	
	/**
	 * FlashVars:
	 * @parameter: swfToBoot			path of target swf
	 * @parameter: idSwfToBoot		id of target swf, will show while loading
	 * ...	wibrst.loading.BootstrapMgr
	 * @author wibrst@qq.com
	 */
	public class BootstrapMgr extends LoadHostBase{
		static public const SWF_LOADED:String = "swfLoaded";
		
		/**
		 * 
		 * @param	vHost host 
		 * @param	VUISwfLoadingNorm sub class of wibrst.loading.VUISwfLoading
		 */
		public function BootstrapMgr( vHost:DisplayObjectContainer, VUISwfLoadingNorm:Class, uSwfDefault:String ) {
			this._uSwfDefault = uSwfDefault;
			this._VUISwfLoadingNorm = VUISwfLoadingNorm;
			this._vHost = vHost;
			
			if ( this._vHost.stage ) {
				inStageHandler ( null ) ;
			}else {
				this._vHost.addEventListener( Event.ADDED_TO_STAGE, inStageHandler );
			}
		}
		
		private function inStageHandler(e:Event):void {
			this._vHost.removeEventListener ( Event.ADDED_TO_STAGE, inStageHandler ) ;
			
			var loaderInfoStage:LoaderInfo = this._vHost.stage.loaderInfo;
			
			this._vHost.stage.align = StageAlign.TOP_LEFT;
			this._vHost.stage.scaleMode = StageScaleMode.NO_SCALE;
			
			/// read and dump or apply flash vars parameters
			this._sUrl = EnvUtil.getLoaderPara( loaderInfoStage, "swfToBoot", this._uSwfDefault );
			this._vHost.stage.frameRate = Number( EnvUtil.getLoaderPara( loaderInfoStage, "frameRate", "24" ) );
			
			var w:Number = Number( EnvUtil.getLoaderPara( loaderInfoStage, "stageWidth", this._vHost.stage.stageWidth.toString() ) );
			var h:Number = Number( EnvUtil.getLoaderPara( loaderInfoStage, "stageHeight", this._vHost.stage.stageHeight.toString() ) );
			
			GlbLayout.setStageWH( w, h );
			
			this._sId = EnvUtil.getLoaderPara( loaderInfoStage, "idToBoot", this._sUrl );
			this._sPrompt = EnvUtil.getLoaderPara( loaderInfoStage, "promptToBoot", this._sId );
			
			this.setVClsAndLoad( this._VUISwfLoadingNorm );
		}
		
		public function setVClsAndLoad( CVUISwfLoading:Class ):void{
			super.setVClsLoading ( CVUISwfLoading, this._vHost, GlbLayout.POINT_CEN ) ;
			var vcLoadUnit:Vector.<LoadUnit> = new Vector.<LoadUnit>;
			vcLoadUnit.push ( new LoadUnit( this._sUrl, this._sId ) ) ;
			super.loadContent( vcLoadUnit );
		}
		
		protected override function loadThreadCompHandler( loading:VUIURLLoading ):void {
			trace( this, "loadThreadCompHandler:", loading.sIdSwfLoad );
			
			var uldr:URLLoader = loading.getUldr(0);
			switch( loading.sIdSwfLoad ) {
				
				case this._sId:
					super.dispatchEvent ( new EDataTransfer ( SWF_LOADED, uldr ) ) ;
				break;
				
			}
		}
		
		private var _sId:String;
		private var _sUrl:String;
		private var _vHost:DisplayObjectContainer;
		private var _VUISwfLoadingNorm:Class;
		private var _uSwfDefault:String;
		private var _sPrompt:String;
		
	}

}